<template>
  <el-main>

    <el-form :inline="true" :model="user" ref="user">

      <el-form-item label="登录名称" prop="phone">
        <el-input v-model="user.phone" placeholder="请输入登录名称"></el-input>
      </el-form-item>

      <el-form-item label="手机验证码" prop="imgcode">
        <el-input v-model="user.imgcode" placeholder="请输入手机验证码"></el-input>

      </el-form-item>
      <el-button @click="getPhoneMessage">获取验证码</el-button>

      <el-row>
        <el-button @click="login" type="success">登录</el-button>

        <router-link to="/login">账户登录</router-link>

      </el-row>
    </el-form>
  </el-main>
</template>

<script>


export default {
  data() {
    return {
      user: {
        phone: '',
        imgcode: '',//验证
      }
    }
  },
  methods: {
    getPhoneMessage() {
      this.axios.get("http://127.0.0.1:8090/auth/getPhoneMessage?phone="+this.user.phone)
          .then(resp=>{
            alert(resp.data.data)
          })
    },login(){
      let user = this.user;
      //发送ajax请求，后台验证用户，如果验证成功，给前台返回一个token（用户id或登录名称）
      this.axios.post("http://127.0.0.1:8090/auth/phonelogin",user).then(res=>{
        if(res.data.data){
          //存储用户信息到localStorege
          this.$cookies.set("tokenId",res.data.data.token)
          this.$router.push("/index")//跳转页面
        }else{
          this.$message(res.data.msg);
        }
      })
    }
    }
}
</script>

<style scoped>
.el-form {
  width: 40%;
  margin-left: 30%;
  margin-top: 10%;
}
</style>
